var api = require("api");
var util=require("../utils/util");

// login获取code
function login_get_openId(){
  return new Promise((resolve,reject)=>{
    wx.login({
      success:function(res){
        if(res.code){
          wx.request({
            url: api.data.requestUrl + api.data.getWxCode,
            header: {
              'content-type': 'application/json;charset=UTF-8'
            },
            method: 'GET',
            data: {code: res.code,type:1},
            success: function (res) {
              resolve(res);
              // util.show_success_toast(res)
            },
            fail: function () {
              reject();
              // util.show_fail_toast()
            },
          })
        }
      }
    })
  })
}
// 解密获取unionId或手机号码
function decrypt_wx(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.decryptWeixinData,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 判断是否登录或token是否过期
function is_login(token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.isLogin,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 微信登录获取token
function wx_login(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.wxLogin,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 判断是否绑定微信
function esti_bind_wx(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.isBindwx,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 绑定微信
function user_bind_wx(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.bindWx,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 绑定手机
function wx_bind_phone(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.wxBindPhone,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 解绑微信
function user_unbind_wx(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.unbindWx,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': wx.getStorageSync('token')?wx.getStorageSync('token'):''
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 退出登录
function out_login(token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.outLogin,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  }) 
}
// 在读年纪列表
function get_grade_list(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGradeList,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 发送验证码
function send_code(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.sendCode,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 验证手机验证码
function very_code(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.veryCode,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 手机验证码登录
function login_by_code(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.loginByCode,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 手机密码登录
function login_by_password(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.loginByPassword,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 获取oss
function get_oss(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getOss,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
//获取全部省份
function get_province_list(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getProvinceList,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
//根据provinceCode获取市
function get_city_list(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getCityList,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 根据市cityCode查询区
function get_area_list(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getAreaList,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 获取防重复提交幂等token
function create_token(token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.createToken,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        // util.show_fail_toast()
      },
    })
  })
}
// 优惠券过期提醒
function get_pref_time(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.prefTimeout,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 首页优惠券活动
function get_activity_pref(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getActivityPref,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        // util.show_fail_toast()
      },
    })
  })
}
// 一键领取
function get_all_pref(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getAllPref,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        // util.show_fail_toast()
      },
    })
  })
}
// 单张领取
function get_one_pref(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getOnePref,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 用户
var activityData=[
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1098189305,888941206&fm=26&gp=0.jpg',name:'爱2你0',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1109140721,610033187&fm=26&gp=0.jpg',name:'睡花醒露',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1160619045,3864890237&fm=26&gp=0.jpg',name:'彼岸朦胧',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1206685284,82164550&fm=26&gp=0.jpg',name:'浪子情结',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1232078298,3831285176&fm=26&gp=0.jpg',name:'情海孤舟',prize:'80元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1294952997,1740987836&fm=26&gp=0.jpg',name:'月夜寒影',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1301460551,3042402209&fm=26&gp=0.jpg',name:'酒自斟',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1308542669,359862243&fm=26&gp=0 (1).jpg',name:'王者',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1308542669,359862243&fm=26&gp=0.jpg',name:'孤君独战',prize:'100元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=131756683,3703581544&fm=26&gp=0.jpg',name:'青丝',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1337959458,2326248370&fm=26&gp=0.jpg',name:'惆怅',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1360681170,1161348926&fm=15&gp=0.jpg',name:'孤冢清风',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=143947979,1941913337&fm=11&gp=0.jpg',name:'zero',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1510922860,1717661462&fm=26&gp=0.jpg',name:'青青芳心',prize:'200元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1596853941,3096031261&fm=26&gp=0.jpg',name:'scx33?',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1643120908,1443984337&fm=26&gp=0.jpg',name:'落雪',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1686931674,2370574014&fm=11&gp=0.jpg',name:'乔治',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1728390348,3201380139&fm=15&gp=0.jpg',name:'太空人',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1729636367,2719399696&fm=26&gp=0.jpg',name:'晴天',prize:'100元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1790045690,3436944432&fm=26&gp=0.jpg',name:'浅唱、',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1813383202,492911046&fm=26&gp=0.jpg',name:'敷衍、',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1828025999,314077970&fm=26&gp=0.jpg',name:'梦回旧景z',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1832963481,49895947&fm=15&gp=0.jpg',name:'Jabin',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1845285694,4024557528&fm=11&gp=0.jpg',name:'春风',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1847587244,3498835388&fm=26&gp=0.jpg',name:'森花',prize:'300元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1848829339,2574293630&fm=26&gp=0.jpg',name:'恋维',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=187937547,4014582017&fm=15&gp=0.jpg',name:'蜗牛',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1882508853,568120619&fm=15&gp=0.jpg',name:'岁月',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1891880870,1841817543&fm=26&gp=0.jpg',name:'淡然',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1906107992,2665439953&fm=26&gp=0.jpg',name:'、淡夏',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=1915028651,40635076&fm=26&gp=0.jpg',name:'盏茶浅抿',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=19245629,1026142199&fm=26&gp=0.jpg',name:'浅相思',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2035291104,1402899574&fm=26&gp=0.jpg',name:'ai',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2044677328,2728762932&fm=26&gp=0.jpg',name:'忘',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=204497437,877718501&fm=26&gp=0.jpg',name:'爆笑气质姐',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=210660538,3708059170&fm=26&gp=0.jpg',name:'萌妹子',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2147049745,3994993561&fm=26&gp=0.jpg',name:'狂热者',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2152374209,1046581644&fm=26&gp=0.jpg',name:'疯子',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2193490822,227787046&fm=26&gp=0.jpg',name:'as',prize:'80元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2193745402,201264850&fm=26&gp=0.jpg',name:'世界',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2202362712,2428593619&fm=26&gp=0.jpg',name:'福',prize:'5元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2252838866,2257689446&fm=26&gp=0.jpg',name:'编号',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2312111397,2180418804&fm=26&gp=0.jpg',name:'逍遥',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2320883706,933400193&fm=26&gp=0.jpg',name:'你好',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2392909859,1496587738&fm=26&gp=0.jpg',name:'二是＊',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2399070948,2585239903&fm=26&gp=0.jpg',name:'骄傲',prize:'10元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2403930721,593109212&fm=26&gp=0.jpg',name:'柠檬脾气',prize:'80元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2488732309,256974990&fm=11&gp=0.jpg',name:'疯子般の我',prize:'20元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2523088456,2367742167&fm=26&gp=0.jpg',name:'偏执',prize:'30元优惠券'},
  {url:'http://ossdev0.oss-cn-shenzhen.aliyuncs.com/NavyHeadPortrait/u=2538207625,199070238&fm=26&gp=0.jpg',name:'范儿',prize:'10元优惠券'}
]
// 查看活动是否过期
function get_activity_type(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getActivityType,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 获取登陆用户的抽奖次数
function get_user_count(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getUserCount,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 用户抽奖
function user_get_money(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.userGetMoney,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 重置抽奖次数
function get_count_after(params,token){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getCountAfterBuy,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 重置抽奖次数
function get_activity_mech(params){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getActivityMech,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}

module.exports={
  login_get_openId,
  decrypt_wx,
  is_login,
  wx_login,
  esti_bind_wx,
  user_bind_wx,
  wx_bind_phone,
  user_unbind_wx,
  out_login,
  get_grade_list,
  send_code,
  very_code,
  login_by_code,
  login_by_password,
  get_oss,
  get_province_list,
  get_city_list,
  get_area_list,
  create_token,
  get_pref_time,
  get_activity_pref,
  get_all_pref,
  get_one_pref,
  activityData,
  get_activity_type,
  get_user_count,
  user_get_money,
  get_count_after,
  get_activity_mech
}